//
//  TMImageShowView.m
//  国学商家管理
//
//  Created by Tao Li on 2018/8/4.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "TMImageShowView.h"
//#import "ZYQAssetPickerController.h"
#import "CollectionViewCell.h"
#import "AddCollectionViewCell.h"
#import "UIAlertController+SPAlert.h"

@interface TMImageShowView()<UICollectionViewDelegate,UICollectionViewDataSource>
@end
@implementation TMImageShowView

- (NSMutableArray *)imageDataArray{
    if (!_imageDataArray) {
        self.imageDataArray = [NSMutableArray array];
    }
    return _imageDataArray;
}
- (void)setIsNetworkImage:(BOOL)isNetworkImage{
    _isNetworkImage = isNetworkImage;
}
- (void)setImageArray:(NSMutableArray *)imageArray{
    _imageArray = imageArray;
    [self.collectionView reloadData];
}
-(void)awakeFromNib{
    [super awakeFromNib];
    self.backgroundColor = [UIColor whiteColor];
    UICollectionViewFlowLayout *flowLayOut = [[UICollectionViewFlowLayout alloc] init];
    if (kWindowW == 320) {
        flowLayOut.itemSize = CGSizeMake(70, 70);
        flowLayOut.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    }else{
    flowLayOut.itemSize = CGSizeMake(80, 80);
    flowLayOut.sectionInset = UIEdgeInsetsMake(15, 11, 0, 11);
    }
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayOut];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.scrollEnabled = NO;
    [self addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self);
        make.right.equalTo(self);
        make.left.equalTo(self);
        make.bottom.equalTo(self);
    }];
    self.i = 0;
    [self.collectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    [self.collectionView registerClass:[AddCollectionViewCell class] forCellWithReuseIdentifier:@"identifier"];
}
#pragma mark ---------collectionView代理方法--------------
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.imageArray.count + 1 ;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    AddCollectionViewCell *cell1 = [collectionView dequeueReusableCellWithReuseIdentifier:@"identifier" forIndexPath:indexPath];
    
    if (self.imageArray.count == 0) {
        return cell1;
        
    }else{
        
        CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
        
        if (indexPath.item + 1 > self.imageArray.count ) {
            return cell1;
        }else{
            if (self.isNetworkImage == YES) {
                [cell.imageV sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithBaseUrl:self.imageArray[indexPath.item]]] placeholderImage:kPlaceholder];
                //[self.imageDataArray addObject:cell.imageV.image];
            }else{
            cell.imageV.image = self.imageArray[indexPath.item];
            }
            [cell.imageV addSubview:cell.deleteButotn];
            cell.deleteButotn.tag = indexPath.item + 100;
            [cell.deleteButotn addTarget:self action:@selector(deleteImage:) forControlEvents:UIControlEventTouchUpInside];
        }
        return cell;
    }
}
- (void)deleteImage:(UIButton *)sender{
    
   UIAlertController *alert =  [UIAlertController alertControllerWithTitle:@"删除图片" message:nil preferredStyle:UIAlertControllerStyleAlert handler:^(UIAlertAction * _Nonnull action) {
       if ([action.title isEqualToString:@"确认"]) {
           NSInteger index = sender.tag - 100;
           //移除显示图片数组imageArray中的数据
           //    if (self.isNetworkImage == YES) {
           //
           //    }
           [self.imageArray removeObjectAtIndex:index];
           if (self.deleteBlock) {
               self.deleteBlock(index);
           }
           //移除沙盒数组中imageDataArray的数据
           [self.collectionView reloadData];
       }else{
           [[SPToolSet currentViewController] dismissViewControllerAnimated:YES completion:nil];
       }
    } cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
    [[SPToolSet currentViewController] presentViewController:alert animated:YES completion:^{
        
    }];
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.item + 1 > self.imageArray.count ) {
        NSLog(@"上传");
        if (self.block) {
            self.block();
        }
       // [self submitPictureToServer];
    }else{
//        ImageViewController *imageViewC =[[ImageViewController alloc] init];
//        //取出存储的高清图片
//        imageViewC.imageData = self.imageDataArray[indexPath.item];
//        [self presentViewController:imageViewC animated:YES completion:nil];
    }
    
}


@end
